package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.HouseUser;
import com.atguigu.service.HouseUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/houseUser")
public class HouseUserController extends BaseController {
    private static final String ACTION_LIST = "redirect:/house/detail/";
    private static final String PAGE_CREATE = "houseUser/create";
    private static final String PAGE_EDIT = "houseUser/edit";
    @Reference
    HouseUserService houseUserService;
    
    @RequestMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable Long houseId, @PathVariable Long id) {
        houseUserService.delete(id);
        return ACTION_LIST + houseId;
    }

    @RequestMapping("/update/{id}")
    public String update(@PathVariable Long id, HouseUser houseUser,HttpServletRequest request){
        HouseUser dbHouseUser = houseUserService.getById(id);
        BeanUtils.copyProperties(houseUser, dbHouseUser);
        houseUserService.update(dbHouseUser);
        return this.successPage(null, request);
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id ,Map map) {
        HouseUser houseUser = houseUserService.getById(id);
        map.put("houseUser", houseUser);
        return PAGE_EDIT;
    }

    
    @RequestMapping("/create")
    public String create(HouseUser houseUser, Map map) {
        map.put("houseUser", houseUser);
        return PAGE_CREATE;
        
    }

    @RequestMapping("/save")
    public String save(HouseUser houseUser, HttpServletRequest request,Map map){
        houseUserService.insert(houseUser);
        return this.successPage(null, request);
    }



}
